@extends "main_not_nav.html"

@block body
<br>
<form class="layui-form">
    {{ csrf_field }}

    <div class="layui-form-item">
        <label class="layui-form-label">目录</label>
        <div class="layui-input-inline">
            <div class="layui-input-inline">
            <select name="pid">
                <option value="0">根目录</option>
                {% for pid in pids %}
                <option value="{{ pid['id'] }}">{{ pid['title'] }}</option>
                {% endfor %}
            </select>
        </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-inline">
            <input type="text" name="title" maxlength="100" lay-verify="required" autocomplete="off" placeholder="请输入内容" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">链接</label>
        <div class="layui-input-inline">
            <input type="text" name="uri" maxlength="100" lay-verify="required" autocomplete="off" placeholder="请输入内容" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

@endblock

@block js
<script>
    //Demo
    layui.use(['form', 'laydate'], function(){
        var form = layui.form;
        form.render('select');

        //监听提交
        form.on('submit(formDemo)', function(data){
            //当前容器的全部表单字段，名值对形式：{name: value}
            // console.log(data.field);
            // layer.msg(JSON.stringify(data.field));
            $.ajax({
                url: "{{ uri }}/store",
                method: "post",
                data: data.field,
                dataType: "JSON",
                success: function (callback){
                    // console.log(callback);
                    layer.msg(callback.message);

                    if (callback.code === 1)
                    {
                        setTimeout(function(){
                            window.parent.location.reload();
                        }, 1000);
                    }
                }
            });

            return false;
        });
    });
</script>

@endblock